package view.user;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;

import controller.ActionEvents.ActionHandler;

import java.awt.Font;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
/**
 * 
 * @author Christian
 * Class Balance.java shows the balance for each account
 */
public class Balance extends JPanel
{
	
	/**
	 * Declares the variables used in the GUI.
	 */
	private JLabel lblMyBalance; // Header
	//private JTable table; // JTable which contains the current users accounts
	
//	TableColumn tableColumn = new TableColumn();
	
	private ActionHandler actionHandler; // Creates an object of ActListener.java
	private JTable table;
	private DefaultTableModel model;
	private JScrollPane scrollPane;
	public String[] columnHeaders = {"ID", "Account", "Balance"};
	private Color tableColors = Color.WHITE;
	private Color tableShadeColors = Color.lightGray;
	
	

	/**
	 * Constructor creates JPanel using CardLayout.
	 * @see Layout managers, CardLayout.
	 * Components is also here specified.
	 */
	public Balance(ActionHandler actionHandler) 
	{
		this.actionHandler = actionHandler;
		setLayout(null);
		setPreferredSize(new Dimension(800, 372));
		setBackground(Color.WHITE);
		
		// Header my balance is created with a JLabel
		lblMyBalance = new JLabel("My balance"); // Creates object lblMyBalance.
		lblMyBalance.setFont(new Font("Euphemia UCAS", Font.PLAIN, 28));
		lblMyBalance.setBounds(321, 6, 163, 39);
		add(lblMyBalance); // Ads JLabel to JPanel.
		
		
		
	}
	/**
	 * Method ads table to frame, when its called.
	 * Ensures that the JTable isnt added to the frame when the model equals null.
	 */
	public void addTable()
	{
		if(table!=null)
			remove(table);
		model = new DefaultTableModel( actionHandler.getFrame().getModelClass().getAccountsRows(), columnHeaders ) 
		{
			Class[] columnTypes = new Class[] 
			{
				String.class, String.class, Integer.class //kolonne datatyper
			};
			public Class getColumnClass(int columnIndex) 
			{
				
				return columnTypes[columnIndex];
				
			}
			boolean[] columnEditables = new boolean[] 
					{
					false, false, false
				};
				public boolean isCellEditable(int row, int column) 
				{
					return columnEditables[column];
				}
		};
		table = new JTable(){
			public Component prepareRenderer(TableCellRenderer renderer,int Index_row, int Index_col) {
				Component comp = super.prepareRenderer(renderer, Index_row, Index_col);
				//even index, selected or not selected
				if (Index_row % 2 == 0 && !isCellSelected(Index_row, Index_col)) {
					comp.setBackground(tableShadeColors);
				} 
				else {
					comp.setBackground(tableColors);
				}
				return comp;
			}
			//Creates an instance of DefaultTableCellRenderer
			 DefaultTableCellRenderer renderCenter = new DefaultTableCellRenderer();

	         {//initializer block
	             renderCenter.setHorizontalAlignment(SwingConstants.CENTER);
	         }

	          @Override
	       public TableCellRenderer getCellRenderer(int Index_row, int Index_col) {
	              return renderCenter; // returns the object of DefaultTableCellRenderer

	       }
			 
		};
		
		JTableHeader header = table.getTableHeader();
		header.setBackground(tableColors);
		table.setModel(model);
		
		table.setVisible(true);
		table.getTableHeader();
		table.setSelectionForeground(Color.BLACK);
		table.getTableHeader().setVisible(true);
		table.getTableHeader().setReorderingAllowed(false);
		//table.setBounds(150, 120, 400,400);
		table.setSize(1000,400);
		
		
		table.getColumnModel().getColumn(0).setPreferredWidth(40);
		table.getColumnModel().getColumn(1).setPreferredWidth(40);
		table.getColumnModel().getColumn(2).setPreferredWidth(100);
		table.setBackground(tableColors);
		table.setSelectionBackground(tableColors);
		table.setGridColor(tableColors);	
		table.getTableHeader().setResizingAllowed(false);
		//add(table); // Ads JTable to JPanel.
		
		
scrollPane = new JScrollPane(table);
		
		//scrollPane.setSize(400,400);
		//scrollPane.setBounds(400, 400, 400, 400);
		scrollPane.setBackground(tableColors);
		scrollPane.getViewport().setBackground(tableColors);
		scrollPane.setBorder(BorderFactory.createEmptyBorder());
		scrollPane.setBounds(25, 60, 700, 400);
		scrollPane.getVerticalScrollBar();
		
		
		add(scrollPane);
		
		
	}
	
	
	/**
	 * @param scrollPane the scrollPane to set
	 */
	public void setScrollPane(JScrollPane scrollPane) {
		this.scrollPane = scrollPane;
	}
	/**
	 * @return the table
	 */
	public JTable getTable() {
		return table;
	}
	/**
	 * @param table the table to set
	 */
	public void setTable(JTable table) {
		this.table = table;
	}
	/**
	 * @return the scrollPane
	 */
	public JScrollPane getScrollPane() {
		return scrollPane;
	}
	/**
	 * @return the model
	 */
	public DefaultTableModel getModel() {
		return model;
	}
	/**
	 * @param model the model to set
	 */
	public void setModel(DefaultTableModel model) {
		this.model = model;
	}
	/**
	 * @param tableColors the tableColors to set
	 */
	public void setTableColors(Color tableColors) {
		this.tableColors = tableColors;
	}
	
	
	
}